package com.wasai;

import java.sql.Timestamp;
import java.util.Random;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.context.transaction.TransactionConfiguration;

import com.wasai.dao.CommentDAO;
import com.wasai.dao.PromotionDAO;
import com.wasai.dao.UserDAO;
import com.wasai.domain.Comment;
import com.wasai.domain.Promotion;
import com.wasai.domain.User;
import com.wasai.util.gravatar.Gravatar;
import com.wasai.util.gravatar.GravatarDefaultImage;

@TransactionConfiguration(defaultRollback = false)
public class SampleDataGenerator extends BaseTest {

	@Resource
	private UserDAO userDAO;

	@Resource
	private PromotionDAO promotionDAO;

	@Resource
	private CommentDAO commentDAO;

	private int size;

	@Before
	public void setUp() {
		this.size = 100;
	}

	@Test
	public void step0DeleteAll() {
		this.deleteFromTables("comment", "promotion", "user");
	}

	@Test
	public void step1GeneratorUser() {
		Gravatar gravatar = new Gravatar();
		gravatar.setDefaultImage(GravatarDefaultImage.IDENTICON);
		for (int i = 0; i < this.size; i++) {
			User user = new User();
			user.setEmail(RandomStringUtils.randomAlphabetic(5) + "@"
					+ RandomStringUtils.randomAlphabetic(5) + "."
					+ RandomStringUtils.randomAlphabetic(3));
			user.setPassword(DigestUtils.md5Hex(RandomStringUtils
					.randomAlphabetic(10)));
			user.setName(RandomStringUtils.randomAlphabetic(5));
			user.setQq(RandomStringUtils.randomAlphabetic(50));
			user.setAvatar(gravatar.getUrl(user.getEmail()));
			userDAO.save(user);
		}
	}

	@Test
	public void step2GeneratorPromotion() {
		final Timestamp now = new Timestamp(System.currentTimeMillis());
		final Random random = new Random();
		for (int i = 0; i < this.size * 2; i++) {
			Promotion promotion = new Promotion();
			promotion.setTitle(RandomStringUtils.randomAlphabetic(20));
			promotion.setDescription(RandomStringUtils.randomAlphabetic(500));
			promotion.setImg(RandomStringUtils.randomAlphabetic(20));
			promotion.setOrgPrice(RandomStringUtils.randomNumeric(2));
			promotion.setProPrice(RandomStringUtils.randomNumeric(2));
			promotion.setUrl(RandomStringUtils.randomAlphabetic(20));
			promotion.setValidDate(new Timestamp(DateUtils.addDays(now,
					0 - random.nextInt(50)).getTime()));
			promotionDAO.save(promotion);
		}
	}

	@Test
	public void step3GeneratorComment() {
		final Timestamp now = new Timestamp(System.currentTimeMillis());
		final Random random = new Random();
		for (int i = 0; i < this.size * 4; i++) {
			Comment comment = new Comment();
			comment.setContent(RandomStringUtils.randomAlphabetic(30));
			comment.setCreateDate(new Timestamp(DateUtils.addDays(now,
					0 - random.nextInt(50)).getTime()));
			comment.setIp(RandomStringUtils.randomNumeric(2) + "."
					+ RandomStringUtils.randomNumeric(2) + "."
					+ RandomStringUtils.randomNumeric(2) + "."
					+ RandomStringUtils.randomNumeric(2));
			User user = (User) this.getRandomEntity(User.class);
			if (user != null) {
				comment.setUser(user);
			}
			Promotion promotion = (Promotion) this
					.getRandomEntity(Promotion.class);
			if (promotion != null) {
				comment.setPromotion(promotion);
			}
			commentDAO.save(comment);
		}
	}
}
